<template>
    <div>
        <div class="editor-title" style="padding: 24px 16px">
            <span class="editor-title__name">{{ data.text }}</span>
        </div>
        <div class="img-btn-con" style="padding: 4px 16px 20px">
            <div class="editor-two__item">
                <div class="editor-two__item-head">标题内容</div>
                <el-input v-model="data.data.title" placeholder="输入标题内容" clearable />
            </div>
            <div class="cls-between" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">文本样式</div>
                <div class="cls-between">
                    <div class="item-con" :class="data.style.weight == 500 ? 'item-con-active' : ''"
                        @click="data.style.weight = 500">常规</div>
                    <div class="item-con" :class="data.style.weight > 500 ? 'item-con-active' : ''"
                        @click="data.style.weight = 600">加粗</div>
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">标签颜色</div>
                <div>
                    <el-color-picker v-model="data.style.title_color" @change="color_change('title_color', '#333')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">背景颜色</div>
                <div>
                    <el-color-picker v-model="data.style.background" @change="color_change('background', '#fff')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 0.5rem">
                <div class="label-con" style="flex: 1">显示更多</div>
                <div>
                    <el-switch v-model="data.style.showMore" width="60px" size="large" inline-prompt active-text="显示"
                        inactive-text="隐藏" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 0.5rem" v-if="data.style.showMore">
                <div class="label-con" style="flex: 1">跳转页面</div>
                <div>
                    <el-select v-model="data.data.jump_url.pageTitle" placeholder="请选择页面" size="small" clearable
                        style="width: 160px" @change="page_change">
                        <el-option v-for="item in pageArr" :key="item.decorate_id" :label="item.title"
                            :value="item.decorate_id" />
                    </el-select>
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 0.5rem">
                <div class="label-con" style="flex: 1">右侧链接箭头</div>
                <div>
                    <el-switch v-model="data.style.arrowSwitch" width="60px" size="large" inline-prompt active-text="显示"
                        inactive-text="隐藏" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">标题左边框</div>
                <div>
                    <el-switch v-model="data.style.showLeft" width="60px" size="large" inline-prompt active-text="显示"
                        inactive-text="隐藏" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem" v-if="data.style.showLeft">
                <div class="label-con" style="flex: 1">左边框颜色</div>
                <div>
                    <el-color-picker v-model="data.style.left_bottom_color"
                        @change="color_change('left_bottom_color', '#333')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">页面边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-left']" :max="max" @change="padding_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-left']" @input="padding_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">框体圆角</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['border-radius']" :max="max" @change="radius_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['border-radius']" @input="radius_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">上边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-top']" :min="0" :max="300" @change="mtt_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-top']" @input="mtt_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">下边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-bottom']" :min="0" :max="300" @change="mtb_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-bottom']" @input="mtb_change" />
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted } from "vue"

const { proxy } = getCurrentInstance()
const max = 30 // 最大值，适用于内外边距和圆角

/**
 * 标题文本
 * @property {Object} pageData  页面数据
 */
const props = defineProps({
    data: {
        type: Object,
        default: {
            id: 12,
            type_name: "title",
            text: "标题文本",
            /** 需要x2转成rpx样式 */
            rpxStyle: {
                "margin-left": 0,
                "margin-right": 0,
                "margin-bottom": 0,
                "margin-top": 0,
                "border-radius": 5,
            },
            data: {
                /**请求地址*/
                reqUrl: "",
                /**请求参数*/
                reqData: {},
                title: "",
                jump_url: {
                    pageTitle: "",
                    page_id: "",
                },
                /* 当没有请求地址的时候，使用data数据 */
                data: [],
            },
        },
    },
})

/**
 * 页面列表
 */
const pageArr = ref([])
onMounted(() => {
    proxy.$axios
        .get("decorate/index")
        .then((r) => {
            let res = r.data
            if (res.code == 0) {
                pageArr.value = res.data
            } else {
                proxy.$message({message:res.msg,type:"error"})
            }
        })
        .catch((err) => { })
})

/**
 * 页面跳转选择
 * @param {Number} page_id 页面跳转ID
 */
const page_change = (page_id) => {
    let page_index = pageArr.value.findIndex((item) => item.decorate_id == page_id)
    let pageTitle = page_index > -1 ? pageArr.value[page_index].title : ""
    props.data.data.data.jump_url = {
        pageTitle: pageTitle,
        page_id: page_id,
    }
}

// 页面边距变化
const padding_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["margin-left"] = props.data.rpxStyle["margin-right"] = Number(val)
}

// 圆角变化
const radius_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["border-radius"] = Number(val)
}

// 上下边距
const mtt_change = (val) => {
    props.data.rpxStyle["margin-top"] = Number(val)
}
const mtb_change = (val) => {
    props.data.rpxStyle["margin-bottom"] = Number(val)
}

/**
 * 当点击清空颜色时，恢复默认颜色
 * @param {*} key 要修改的颜色字段
 * @param {*} color 默认颜色， 当key值为null时，恢复默认颜色
 */
const color_change = (key, color) => {
    if (!props.data.style[key]) {
        props.data.style[key] = color
    }
}
</script>

<style lang="less" scoped>
.editor-two__item {
    margin-bottom: 20px;
}

.editor-two__item-head {
    color: #333;
    line-height: 46px;
}

.label-con {
    flex: none;
    width: 82px;
    font-size: 14px;
    padding-right: 10px;
    color: #969799;

    .label-con__span {
        font-size: 13px;
        color: #999;
    }
}

.item-con {
    color: #323232;
    padding: 7px 14px;
    border: 1px solid #ebedf0;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
}

.item-con-active {
    color: #4b93fa !important;
    background-color: #edf4ff !important;
    border: 1px solid #4b93fa;
}</style>
